package com.health.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.health.system.entity.HealthRules;
import com.health.system.entity.HealthSuggestions;
import com.health.system.mapper.HealthRulesMapper;
import com.health.system.mapper.HealthSuggestionsMapper;
import com.health.system.service.HealthSuggestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2024-06-22
 */
@Service
public class HealthSuggestionsServiceImp extends ServiceImpl<HealthSuggestionsMapper, HealthSuggestions> implements HealthSuggestionsService {

    @Autowired
    private HealthSuggestionsMapper healthSuggestionsMapper;
    @Autowired
    private HealthRulesMapper healthRulesMapper;

    @Override
    public IPage<HealthSuggestions> getAllHealthSuggestions(Long current, Long size) {
        IPage<HealthSuggestions> page = new Page<>(current, size);
        IPage<HealthSuggestions> result = healthSuggestionsMapper.selectPage(page, null);

        for (HealthSuggestions record : result.getRecords()) {
            Integer stars = healthRulesMapper.selectOne(new LambdaQueryWrapper<HealthRules>().eq(HealthRules::getId, record.getHealthRuleId())).getStars();
            record.setStars(stars);
        }

        return result;
    }

    @Override
    public HealthSuggestions getHealthSuggestionsById(Integer id) {
        return healthSuggestionsMapper.selectById(id);
    }

    @Override
    public List<HealthSuggestions> getHealthSuggestionsByStars(String stars) {
        QueryWrapper<HealthSuggestions> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stars", stars);
        return healthSuggestionsMapper.selectList(queryWrapper);
    }

    @Override
    public void addHealthSuggestions(HealthSuggestions healthSuggestions) {
        healthSuggestionsMapper.insert(healthSuggestions);
    }

    @Override
    public void updateHealthSuggestions(HealthSuggestions healthSuggestions) {
        healthSuggestionsMapper.updateById(healthSuggestions);
    }

    @Override
    public void deleteHealthSuggestions(Integer id) {
        healthSuggestionsMapper.deleteById(id);
    }
}
